import { cqrs, fetcher } from '../utils';

export async function queryDynamicComponent(
  componentCode: string,
): Promise<Record<string, any>> {
  return cqrs.queryV2({
    body: { code: 'DynamicComponentByCodeOneQuery', params: { componentCode } },
  });
}

export async function emitComponentUse(component: Record<string, any>) {
  if (!component?.id) {
    return;
  }
  // const ip: any = null;
  // const ip = await initLoadIp();
  // if (ip.error) {
  //   console.warn('ip获取错误', ip.error);
  // }
  // {
  //   "status": "success",
  //   "country": "中国",
  //   "countryCode": "CN",
  //   "region": "SC",
  //   "regionName": "四川省",
  //   "city": "成都",
  //   "zip": "",
  //   "lat": 30.6498,
  //   "lon": 104.0555,
  //   "timezone": "Asia/Shanghai",
  //   "isp": "China Unicom CHINA169 Network",
  //   "org": "China United Network Communications Corporation Limited",
  //   "as": "AS4837 CHINA UNICOM China169 Backbone",
  //   "query": "116.169.100.87"
  // }

  // const ipAddress = ip?.ip || '0.0.0.0';
  // const ipCountry = ip?.country;
  // const ipProvince = ip?.region;
  // const ipCity = ip?.city;
  // const ipAddr = `${ip?.country || ''}${ip?.region || ''}${ip?.city || ''}`;
  // const ipIsp = ip?.isp;
  // const ipLongitude = ip?.lon;
  // const ipLatitude = ip?.lat;
  if (!component?.isPage) {
    return;
  }
  const body = {
    componentId: component?.id,
    isPage: component?.isPage,
    // ipCountry,
    // ipProvince,
    // ipCity,
    // ipAddr,
    // ipIsp,
    // ipLongitude,
    // ipLatitude,
  };
  await fetcher('/api/dde/cqrs', {
    body: { code: 'DynamicComponentRecordCreate', params: { data: body } },
  });
}
